package com.nulldev.util.scripting.ScriptAPI.baseJS.testing;

import com.nulldev.util.JVM.Arguments;
import com.nulldev.util.JVM.JVM;
import com.nulldev.util.scripting.ScriptAPI.baseJS.legacy.lexer.BaseJSEnvironment;
import com.nulldev.util.scripting.ScriptAPI.baseJS.legacy.lexer.BaseJSFunction;
import com.nulldev.util.scripting.ScriptAPI.baseJS.legacy.lexer.BaseJSInterp;
import com.nulldev.util.scripting.ScriptAPI.baseJS.legacy.lexer.evaler.BaseJSEvaluator;

public class BaseJSTesting {

	public static final String V8_NTUBE_PAYLOAD = "var Ew={yJ:function(a,b){a.splice(0,b)},"
			+ "QX:function(a,b){var c=a[0];a[0]=a[b%a.length];a[b%a.length]=c},"
			+ "AX:function(a){a.reverse()}};_NG=function(a){a=a.split(\"\");Ew.AX(a,52);Ew.yJ(a,3);Ew.QX(a,1);Ew.QX(a,67);Ew.yJ(a,2);Ew.QX(a,8);return a.join(\"\")};";
	public static final String V8_NTUBE_EXPECTED_RESULT = "AOq0QJ8wRgIhAMnPWGLB5wSHkhieIABHg12r3DwPIEkLhWVxyemQkrr-AiEApXwyZUyz6Rg06vMhcrJnrA4IKrNYQ8oOOBx10EV0dec=";
	public static final String V8_NTUBE_SUBMISSION_ITEM = "=ced0VE01xBOOo8QYNrKI4ArnJrchMv60gR6zyRZywXpAEiA-rrkQmeyxVWhLkEIPwD3r21gHBAIeihkHSw5BLGWPnMAhIgAw8JQ0qORUORUO";

	public static void main(String[] args) throws Exception {
		Arguments.setup(args);
		if (args.length == 1 && args[0].equals("--profile")) {
			Thread.sleep(1000L * 60L);
		}
		JVM.println("=> Running test: nTube-v8-decipherer");
		JVM.println("-> Using backend: legacy-stable");
		try {
			BaseJSEnvironment f;
			final long start0 = System.currentTimeMillis();
			f = BaseJSInterp.get().interpret(V8_NTUBE_PAYLOAD);
			final long end0 = System.currentTimeMillis();
			JVM.println("[0/1] Pre-process: " + (end0 - start0) + " ms");
			final long start1 = System.currentTimeMillis();
			final Object c = ((BaseJSFunction) f.get("_NG")).run(false, f, V8_NTUBE_SUBMISSION_ITEM);
			final long end1 = System.currentTimeMillis();
			JVM.println("[1/1] Execute: " + (end1 - start1) + " ms");
			JVM.println("-> Result: " + c);
			final boolean passed = c.equals(V8_NTUBE_EXPECTED_RESULT);
			JVM.println("Test result: " + (passed ? "PASSED" : "FAILED"));
			JVM.println("Total Execution Time: " + ((end0 - start0) + (end1 - start1)) + " ms.");
		} catch (Exception ex) {
			JVM.println("[0/0] Failed!");
			JVM.errorln(ex);
		}
		JVM.println("-> Using backend: new");
		{
			BaseJSEnvironment f = new BaseJSEnvironment(true);
			final long start0 = System.currentTimeMillis();
			BaseJSEvaluator.eval(f, V8_NTUBE_PAYLOAD);
			final long end0 = System.currentTimeMillis();
			JVM.println("[0/1] Pre-process: " + (end0 - start0) + " ms");
			final long start1 = System.currentTimeMillis();
			final Object c = ((BaseJSFunction) f.get("_NG")).setDebugging(true).run(true, f, V8_NTUBE_SUBMISSION_ITEM);
			final long end1 = System.currentTimeMillis();
			JVM.println("[1/1] Execute: " + (end1 - start1) + " ms");
			JVM.println("-> Result: " + c);
			final boolean passed = c.equals(V8_NTUBE_EXPECTED_RESULT);
			JVM.println("Test result: " + (passed ? "PASSED" : "FAILED"));
			JVM.println("Total Execution Time: " + ((end0 - start0) + (end1 - start1)) + " ms.");
		}
	}
}
